home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 5_2.lha / 5_2 / 5_2b1.c < prev    next >
Text File  |  1993-08-08  |  813b  |  40 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / insert a new node to the left, right
  6. / or at the node pointed to by "head"
  7. include <tree.h>
  8. include <string.h>
  9.  
  10. tatic void insnode(tnode **parent, char *str)
  11.  
  12.    // a leaf found, enter the node here
  13.    if (!*parent)
  14. {
  15. *parent = new tnode(str);
  16. return;
  17. }
  18.  
  19.    // check the name
  20.    int cmp = strcmp(str, (*parent)->tword);
  21.  
  22.    // enter in the left subtree
  23.    if (cmp < 0)
  24. insnode(&(*parent)->left, str);
  25.  
  26.    // enter in the right subtree
  27.    else if (cmp > 0)
  28. insnode(&(*parent)->right, str);
  29.  
  30.    // equal, increment the reference count
  31.    else /* if (cmp == 0) */
  32. (*parent)->count++;
  33.  
  34.  
  35. / add a new word to the tree
  36. oid tree:: addnode(char *str)
  37.  
  38.    insnode(&head, str);
  39.  
  40.